package com.easylinkin.linkappapi.device.util;

import cn.hutool.core.collection.CollectionUtil;
import com.easylinkin.linkappapi.config.entity.Config;
import com.easylinkin.linkappapi.config.service.ConfigService;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.facerecognition.util.SpringContextUtil;
import java.util.Arrays;
import java.util.List;
import org.springframework.util.StringUtils;

/**
 * @author xiaoyang
 * @version 1.0
 * @date 2022/08/09
 * @description
 */
public class DeviceUtil {

  private static ConfigService configService = SpringContextUtil.getBean(ConfigService.class);

  /**
   * 判断是否属于指定key下的设备类型
   * @param device
   * @param deviceTypeKey
   * @return
   */
  public static boolean isBelongSpecifiedDeviceType(Device device, String deviceTypeKey) {
    if (null == device || null == device.getDeviceUnit()) {
      return false;
    }
    if(null == device.getTenantId()){
      return false;
    }
    String tenantId = device.getTenantId();
    List<String> deviceTypeNameList = getDeviceTypeNamesByKey(deviceTypeKey,tenantId);
    if (CollectionUtil.isEmpty(deviceTypeNameList)) {
      return false;
    }
    if (deviceTypeNameList.contains(device.getDeviceUnit().getDeviceTypeName())) {
      return true;
    }
    return false;
  }

  /**
   * 获取指定key的设备类型列表
   * @param deviceTypeKey
   * @return
   */
  public static List<String> getDeviceTypeNamesByKey(String deviceTypeKey,String tenantId) {
    //获取对应的型号
    Config configServiceOneByKey = configService.getOneByKeyAndTenantId(deviceTypeKey,tenantId);//喷淋
    if (null == configServiceOneByKey) {
      return null;
    }

    String deviceTypeNamesStr = configServiceOneByKey.getValue();
    if (StringUtils.isEmpty(deviceTypeNamesStr)) {
      return null;
    }
    String[] split = deviceTypeNamesStr.split(",");
    List<String> deviceTypeNameList = Arrays.asList(split);
    return deviceTypeNameList;
  }
}
